// pages/store_manager/store_manager.js
const config = require('../../config.js');
const utils = require('../../utils/util.js');
const request = require('../../utils/request.js')
import { getStoreDetail, editStoreData, uploadQnImage } from '../../static/api/api.js'
let cropperObj=null;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shopDetail:null,
        uploadImg:[],
        uploadType:1,
        cropperParams:{
            width:350,
            height:350,
            src:'',
            hasClip:false
        },
        isShowCropper:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getStoreDetail()
        cropperObj = this.selectComponent("#imageCropper");
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
       
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let that = this;
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    inputValueChange(e){
        utils.inputValueChange(e,this)
    },
    //获取店铺基本细信息
    getStoreDetail(e) {
        wx.showLoading({
            title: '加载中...',
        })
        getStoreDetail({}).then(response => {
            wx.hideLoading();
            let img = [];
            this.setData({
                'shopDetail': response.data,
                'uploadImg': response.data.other_img==''?[] : response.data.other_img.split(',')
            })
        }).catch(response => {
            wx.hideLoading();
        })
    },
    //编辑店铺信息
    editStoreData(e) {
        console.log(utils.isPhone(this.data.service_phone))
        if (!utils.isPhone(this.data.shopDetail.service_phone)){
            wx.showToast({
                icon:'none',
                title: '请输入正确的店铺号码',
            })
            return;
        }
        wx.showLoading({
            title: '加载中...',
        })
        if (this.data.uploadImg.length>0){
            this.setData({
                'shopDetail.other_img': this.data.uploadImg.join(',')
            })
        }
        editStoreData(this.data.shopDetail).then(response => {
            wx.hideLoading();
            wx.showToast({
                title: '修改成功',
            })
            wx.navigateBack({
                
            })
        }).catch(response => {
            wx.hideLoading();
        })
    },
    chooseImage(e) {
        let that = this;
        cropperObj = null
        cropperObj = this.selectComponent("#imageCropper");
        cropperObj.upload(function () {
            that.setData({
                'isShowCropper': true,
            })
        }, function () {
            that.setData({
                'isShowCropper': false,
            })
        });
        that.setData({
            uploadType: 1
        })
    },
    clipCropperImg(){
        let that = this;
        console.log('裁剪的图片');
        wx.showLoading({
            title: '图片处理中...',
        })
        cropperObj.getImg(function(res){
            wx.hideLoading()
            console.log(res);
            that.uploadImg(res.url,function(data){
                console.log(data)
                that.setData({
                    isShowCropper: true
                })
                if (that.data.uploadType == 1){
                    that.setData({
                        'shopDetail.main_img': data.data.url,
                        'isShowCropper': false
                    })
                }else{
                    that.data.uploadImg.push(data.data.url)
                    let images = that.data.uploadImg
                    console.log(images)
                    // 限制最多只能留下3张照片
                    that.setData({
                        uploadImg: images.length <= 3 ? images : images.slice(0, 3),
                        'isShowCropper': false
                    })
                }
                
            })
        })
    },
    cropperload(e) {
        console.log("cropper初始化完成");
    },
    loadimage(e) {
        cropperObj.imgReset();
    },
    cancelClipBtn() {
        this.setData({
            'isShowCropper': false,
        })
        this.loadimage()
    },
    uploadShopOtherImg(){
        let that = this;
        cropperObj = null
        cropperObj = this.selectComponent("#imageCropper");
        cropperObj.upload(function () {
            that.setData({
                'isShowCropper': true,
            })
        }, function () {
            that.setData({
                'isShowCropper': false,
            })
        });
        that.setData({
           
            uploadType: 2
        })
        
        return;
        // wx.chooseImage({
        //     count: 1,
        //     sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
        //     sourceType: ['album', 'camera'], //可选择性开放访问相册、相机
        //     success: res => {
        //         this.uploadImg(res.tempFilePaths[0], function (data) {
        //             that.data.uploadImg.push(data.data.url)
        //             let images = that.data.uploadImg
        //             console.log(images)
        //             // 限制最多只能留下3张照片
        //             that.setData({
        //                 uploadImg: images.length <= 3 ? images : images.slice(0, 3)
        //             })
        //         })

        //     }
        // })
    },
    //删除图片
    delImage(e) {
        let dataset = e.currentTarget.dataset;
        console.log(dataset)
        let images = this.data.uploadImg;
        images.splice(dataset.index, 1)
        this.setData({
            uploadImg: images
        })
    },
    //上传图片
    uploadImg(path,cb) {
        let that = this;
        setTimeout(function () {
            var urls = config.interfaceHost + "/api.php/common/Qiniu/upLoadImg";
            console.log(path);
            wx.showLoading({
                title: '图片处理中..',
            })
            wx.uploadFile({
                url: urls,
                filePath: path,
                name: 'image',
                header: {
                    'content-type': 'multipart/form-data',
                    'site': config.site,
                    'os': config.os,
                    'token': wx.getStorageSync('token')
                },
                success: function (res) {
                    wx.hideLoading()
                    var str = res.data;
                    console.log(res.data);
                    if(typeof cb == 'function'){
                        cb(JSON.parse(res.data));
                    }
                    
                },
                fail: function (res) {
                    wx.hideLoading()
                    console.log(res);
                    wx.showModal({
                        title: '提示',
                        content: "网络请求失败，请确保网络是否正常",
                        showCancel: false,
                        success: function (res) {

                        }
                    });
                    wx.hideToast();
                }
            });
        }, 500)
    },
    //预览图片
    handleImagePreview(e) {
        const idx = e.target.dataset.index
        const images = this.data.uploadImg
        wx.previewImage({
            current: images[idx], //当前预览的图片
            urls: images, //所有要预览的图片
        })
    },
})